package org.example.admin.controller.lide;

import org.example.admin.entity.Comment;
import org.example.admin.service.CommentService;
import org.example.util.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/com")
public class CommentController {

    @Resource
    private CommentService commentService;

    @GetMapping("/show")
    public R show(@RequestParam(value = "storeid") int storeid){

        List<Comment> com = commentService.selectCommentBystoreId(storeid);
        System.out.println(com);
        if(com == null){
            R.fail("查询失败");
        }
        return R.ok(com);
    }

    @PostMapping("/insert")
    public R insert(@RequestBody Comment com){

        if(com == null){
            R.fail("实例不存在");
        }
        int sign = commentService.insertComment(com.getCommentcontent(),
                                                com.getStoreid(),
                                                com.getUserid());
        if(sign <= 0){
            R.fail("添加错误");
        }
        List<Comment> com1 = commentService.selectCommentAll();
        int sign1 = 0;
        for(Comment com2 : com1){
            sign1 = com2.getCommentid();
        }
        return R.ok(sign1);
    }

    @PostMapping("/update")
    public R update(@RequestParam(value = "content") String content,
                    @RequestParam(value = "commentid") int commentid){

        Comment com = commentService.selectCommentById(commentid);
        if(com == null){
            return R.fail("评论不存在");
        }
        int sign = commentService.updateContent(content, commentid);
        if(sign <= 0){
            return R.fail("修改失败");
        }
        return R.ok();
    }

    @PostMapping("/delete")
    public R delete(@RequestParam(value = "commentid") int commentid){

        Comment com = commentService.selectCommentById(commentid);
        if(com == null){
            return R.fail("评论不存在");
        }
        int sign = commentService.deleteComment(commentid);
        if(sign <=0 ){
            return R.fail("删除评论失败");
        }
        return R.ok();
    }
}
